import api from './api';
import { Class, Student } from '@/types';

// 接口返回类型
interface ApiResponse<T = any> {
  code: number;
  message: string;
  data?: T;
}

interface ClassListResponse {
  classes: Class[];
}

interface StudentListResponse {
  students: Student[];
  total: number;
}

// 获取所有班级列表
export const getClasses = async (): Promise<ApiResponse<ClassListResponse>> => {
  try {
    console.log('获取班级列表');
    const response = await api.get('/teacher/class/list');
    console.log('班级列表响应:', response);
    
    // 如果响应中没有classes字段，则构造一个标准格式的响应
    if (response.code === 2000 && response.data && !response.data.classes) {
      // 确保返回格式统一为 {data: {classes: []}}
      return {
        code: response.code,
        message: response.message,
        data: {
          classes: Array.isArray(response.data) ? response.data : []
        }
      };
    }
    
    return response;
  } catch (error) {
    console.error('获取班级列表错误:', error);
    throw error;
  }
};

// 获取所有班级列表 (用于签到统计)
export const getAllClasses = async (): Promise<ApiResponse<ClassListResponse>> => {
  try {
    const response = await api.get('/teacher/class/all');
    console.log('获取所有班级列表响应:', response);
    
    // 如果响应中没有classes字段，则构造一个标准格式的响应
    if (response.code === 2000 && response.data && !response.data.classes) {
      // 确保返回格式统一为 {data: {classes: []}}
      return {
        code: response.code,
        message: response.message,
        data: {
          classes: Array.isArray(response.data) ? response.data : []
        }
      };
    }
    
    return response;
  } catch (error) {
    console.error('获取所有班级列表错误:', error);
    throw error;
  }
};

// 绑定课程班级
export const bindCourseClass = async (courseId: number, classIds: number[]): Promise<ApiResponse> => {
  try {
    console.log('绑定课程班级:', { courseId, classIds });
    const response = await api.post('/teacher/course_class/bind', { courseId, classIds });
    return response;
  } catch (error) {
    console.error('绑定课程班级错误:', error);
    throw error;
  }
};

// 解绑课程班级
export const unbindCourseClass = async (courseId: number, classId: number): Promise<ApiResponse> => {
  try {
    const response = await api.post('/teacher/course_class/unbind', { courseId, classId });
    return response;
  } catch (error) {
    console.error('解绑课程班级错误:', error);
    throw error;
  }
};

// 获取班级学生列表
export const getStudentsByClass = async (classId: number): Promise<ApiResponse<{ students: any[] }>> => {
  try {
    console.log('获取班级学生列表:', classId);
    const response = await api.get(`/teacher/student/list?class_id=${classId}`);
    console.log('班级学生列表响应:', response);
    return response;
  } catch (error) {
    console.error('获取班级学生列表错误:', error);
    throw error;
  }
};

// 创建学生
export const createStudent = async (studentId: string, name: string, password: string = '123456'): Promise<ApiResponse> => {
  try {
    console.log('创建学生:', { studentId, name, password });
    const response = await api.post('/teacher/student/create', { 
      studentId, 
      name,
      password
    });
    console.log('创建学生响应:', response);
    return response;
  } catch (error) {
    console.error('创建学生错误:', error);
    throw error;
  }
};

// 更新学生信息
export const updateStudent = async (id: number, studentId: string, name: string, classId: number): Promise<ApiResponse> => {
  try {
    console.log('更新学生:', { id, studentId, name, classId });
    const response = await api.post('/teacher/student/update', { 
      id, 
      studentId, 
      name, 
      classId 
    });
    return response;
  } catch (error) {
    console.error('更新学生错误:', error);
    throw error;
  }
};

// 删除学生
export const deleteStudent = async (id: number): Promise<ApiResponse> => {
  try {
    console.log('删除学生:', id);
    const response = await api.post('/teacher/student/delete', { id });
    return response;
  } catch (error) {
    console.error('删除学生错误:', error);
    throw error;
  }
};

// 通过班级ID获取学生列表
export const getStudentsByClassID = async (classId: number): Promise<ApiResponse<{class_name: string, students: Student[]}>> => {
  try {
    console.log('获取班级学生列表:', classId);
    const response = await api.get(`/teacher/class/students?class_id=${classId}`);
    console.log('班级学生列表响应:', response);
    return response;
  } catch (error) {
    console.error('获取班级学生列表错误:', error);
    throw error;
  }
}; 